package net.zdreamer.shunyi.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import net.zdreamer.shunyi.R;
import net.zdreamer.shunyi.adapter.GuangChangAdapter;
import net.zdreamer.shunyi.myview.XListView;
import net.zdreamer.shunyi.utils.ImageUtil;
import net.zdreamer.shunyi.utils.ListUtils.GuangChangInfos;

import android.os.Handler;

public class JiaoYouActivity extends Activity implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
    private ImageView photo, imgback;
    private RelativeLayout jiaoyouback;
    private LinearLayout guangchang;
    private XListView jiaoyoulist;
    private GuangChangInfos guangChangInfos;
    private GuangChangAdapter mAdapter;
    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiao_you);
        jiaoyoulist = (XListView) findViewById(R.id.jiaoyoulist);
        jiaoyoulist.addHeaderView(inithead());
        jiaoyoulist.setOnItemClickListener(this);
        jiaoyoulist.setXListViewListener(this);
        jiaoyoulist.setPullLoadEnable(true);

        guangChangInfos = new GuangChangInfos();
        mAdapter = new GuangChangAdapter(this, guangChangInfos.guangChangInfos, R.layout.guangchamg_item);
        jiaoyoulist.setAdapter(mAdapter);

        mHandler = new Handler();
    }

    private View inithead() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.jiaoyouhead, null);


        photo = (ImageView) view.findViewById(R.id.jiaoyouphoto);
        photo.setImageBitmap(ImageUtil.toRoundBitmap(this, R.drawable.jiaoyouphoto));
        jiaoyouback = (RelativeLayout) view.findViewById(R.id.jiaoyouback);
        guangchang = (LinearLayout) view.findViewById(R.id.guangchanglayout);
        imgback = (ImageView) view.findViewById(R.id.guangchangback);
        imgback.getBackground().setAlpha(100);
        ImageUtil.appBlur(JiaoYouActivity.this, guangchang, jiaoyouback);
        return view;
    }

    private void onLoad() {
        jiaoyoulist.stopRefresh();
        jiaoyoulist.stopLoadMore();
        jiaoyoulist.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                start = ++refreshCnt;
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
